#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

int ReadDir(char* filename){
	
	struct dirent **namelist;
	struct stat fileattr;
	int n=0;
	char filename1[256];
	
	n=scandir(filename,&namelist,0,alphasort);
	if(n<0){
		perror("scandir error");
		exit(1);
	}else{
		while(n--){
			strcpy(filename1,filename);
			strcat(filename1,"/");
			strcat(filename1,namelist[n]->d_name);
			if(stat(filename1,&fileattr)==-1){
				perror("can not read file's attrbute");
				exit(1);
			}else{
				if(S_ISREG(fileattr.st_mode)){
					printf("%s\n",filename1);
				}else if(S_ISDIR(fileattr.st_mode)){
					if((strcmp(namelist[n]->d_name,".")!=0)&&(strcmp(namelist[n]->d_name,"..")!=0)){
					printf("%s\n",filename1);
					ReadDir(filename1);
					}
				}
		  }		
		 free(namelist[n]);
		}
		free(namelist);
	}
	return 0;
}

int main(void)
{
	ReadDir("/home/walter/codes");
	return 0;
}
